package com.minhua.jijiehao2.mainframe.account.accountFragment.impl

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.AbsListView
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.abslistview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.AccountBean
import com.minhua.jijiehao2.bean.AllAccountBean
import com.minhua.jijiehao2.mainframe.account.accountFragment.imp.IAccountView
import com.minhua.jijiehao2.mainframe.account.channellistactivity.impl.ChannelListActivity
import com.minhua.jijiehao2.utils.T
import com.minhua.jijiehao2.view.AccountDetailDialog
import kotlinx.android.synthetic.main.fragment_account.*

class AccountFragment : BaseFragment<IAccountView, AccountPresenter>(), IAccountView {
    override fun getLayoutId(): Int = R.layout.fragment_account

    override fun getPresenter(): AccountPresenter = AccountPresenter()

    companion object {

        fun newInstance(): AccountFragment {
            val fragment = AccountFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
//            R.id.ivRight -> startActivity(Intent(activity, ShareActivity::class.java))
            R.id.addAccount, R.id.ivRight -> startActivity(Intent(activity, ChannelListActivity::class.java))
        }
    }


    private val listAccount = arrayListOf<AccountBean>()
    private var accountAdapter: BaseCommonAdapter<AccountBean>? = null
    private var headerViewHolder: ViewHolder? = null
    private var footerViewHolder: ViewHolder? = null

    override fun initView() {
        ctTitle.viewHolder.setOnClickListener(R.id.ivRight, mOnClickListener)
        accountAdapter = object : BaseCommonAdapter<AccountBean>(activity, R.layout.item_account, listAccount) {
            override fun convert(holder: ViewHolder?, t: AccountBean?) {
                lvContent.turnToNormal()
                val mOnClickListener = View.OnClickListener { v ->
                    when (v.id) {
                        R.id.tvGoldDetail -> showDetailDialog(t)
                        R.id.tvDelete -> {
                            lvContent.turnToNormal()
                            Handler().postDelayed({ mPresenter.doDelete(t?.id!!) }, 300)
                        }
                        R.id.tvPay -> {
                            lvContent.turnToNormal()
                            Handler().postDelayed({ mPresenter.doRepay(t?.id!!, t.repayStatus) }, 300)
                        }
                    }
                }
                holder?.setOnClickListener(R.id.tvGoldDetail, mOnClickListener)
                holder?.setOnClickListener(R.id.tvDelete, mOnClickListener)
                holder?.setOnClickListener(R.id.tvPay, mOnClickListener)

                holder?.setText(R.id.tvGoldName, t?.pName + "(" + t?.ordersName + ")")
                holder?.setText(R.id.tvGoldCount, t?.amtByMonth.toString())
                holder?.setText(R.id.tvGoldDay, t?.remainRepayDay)
                Glide.with(activity).load(t?.picUrl).into(holder?.getView(R.id.ivGoldIcon))

                val repayStatus = t?.repayStatus
                if (repayStatus == 0) {
                    holder?.setVisible(R.id.tvGoldStatus, View.INVISIBLE)
                } else {
                    holder?.setVisible(R.id.tvGoldStatus, View.VISIBLE)
                }

            }
        }
        headerViewHolder = ViewHolder.get(activity, null, null, R.layout.layout_account_header, 0)
        val layoutParams = AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)
        headerViewHolder?.convertView?.layoutParams = layoutParams
        lvContent.addHeaderView(headerViewHolder?.convertView, null, true)
        lvContent.setHeaderDividersEnabled(false)


        footerViewHolder = ViewHolder.get(activity, null, null, R.layout.layout_account_footer, 0)
        val layoutParamsFooter = AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)
        footerViewHolder?.convertView?.layoutParams = layoutParamsFooter
        footerViewHolder?.setOnClickListener(R.id.addAccount, mOnClickListener)
        lvContent.addFooterView(footerViewHolder?.convertView, null, true)
        lvContent.setFooterDividersEnabled(false)

        lvContent.adapter = accountAdapter

        refresh.setOnRefreshListener { mPresenter.getAccountList() }
    }

    /**
     * 展示详情dialog
     */
    private var dialog: AccountDetailDialog? = null

    private fun showDetailDialog(t: AccountBean?) {
        if (dialog == null) {
            dialog = AccountDetailDialog(activity)
        }
        dialog?.setData(t)
        dialog?.show()
    }

    override fun initData() {
        mPresenter.getAccountList()
    }

    private var isFirstIn = true
    override fun onResume() {
        super.onResume()
        if (isFirstIn) {
            isFirstIn = false
        } else {
            if (!isHidden) {
                initData()
            }
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            initData()
        }
    }

    override fun onAccountListSuccess(allAccountBean: AllAccountBean) {
        refresh.isRefreshing = false
        headerViewHolder?.setText(R.id.tvCountGold, allAccountBean.totalAmt.toString())
        listAccount.clear()
        listAccount.addAll(allAccountBean.accountRecords)
        accountAdapter?.notifyDataSetChanged()
        if (allAccountBean.accountRecords.isNotEmpty()) {
            footerViewHolder?.getView<ImageView>(R.id.ivAccountEmpty)?.visibility = View.GONE
        } else {
            footerViewHolder?.getView<ImageView>(R.id.ivAccountEmpty)?.visibility = View.VISIBLE
        }
    }

    override fun onDeleteSuccess() {
        T.showShort("删除成功")
        mPresenter.getAccountList()
    }

    override fun onRepaySuccess() {
        T.showShort("操作成功")
        mPresenter.getAccountList()
    }
}